<template>
	<view>
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">注册</block>
		</cu-custom>
		<view class="login-img">
			<view class="owl-login" :class="hideEyes?'password':''">
				<view class="hand"></view>
				<view class="hand hand-r"></view>
				<view class="arms">
					<view class="arm"></view>
					<view class="arm arm-r"></view>
				</view>
			</view>
		</view>
		<view class="login-form">
			<u-form :model="loginForm" ref="uLoginForm">
				<view class="title" style="text-align: center;">
					<text>注册</text>
				</view>
				<u-form-item prop="userName" :border-bottom=false>
					<u-input v-model="loginForm.userName" :type="userType" placeholder="账号"
						style="background-color:white;" />
				</u-form-item>
				<u-form-item prop="userPwd" :border-bottom=false>
					<u-input v-model="loginForm.userPwd" @focus="passwordF_B" @blur="passwordF_B" :type="Pwdtype"
						placeholder="密码" style="background-color:white;" />
				</u-form-item>
				<u-form-item prop="Phone" :border-bottom=false>
					<u-input v-model="loginForm.Phone" :type="userType" placeholder="手机号"
						style="background-color:white;" />
				</u-form-item>
				<view class="login_register">
					<button type="primary" class="btn_login" shape="square" @click="sureRegister">确定注册</button>
				</view>
			</u-form>

			<!--弹出层-->
			<u-overlay :show="show" @click="show = false">
				<view class="warp">
					<view class="rect" @tap.stop>
						<u-form>
							<view class="title" style="text-align: center;margin-top: 10px;">
								<text style="font-size: 15px;">注册成功，是否直接登录？</text>
							</view>
							<view class="login_register">
								<button type="primary" class="btn_yes" size="small" shape="square" @click="btn_yes">是
								</button>
								<button type="primary" class="btn_no" size="small" shape="square" @click="btn_no">否
								</button>
							</view>
						</u-form>
					</view>
				</view>
			</u-overlay>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				hideEyes: false,
				show: false,
				loginForm: {
					userName: "",
					userPwd: "",
					Phone: ""
				},
				userType: 'text',
				Pwdtype: 'password',
				rules: {
					userName: [
						// 必填规则
						{
							required: true,
							message: '请填写用户名',
							trigger: ['blur', 'change']
						}
					],
					userPwd: [
						// 必填规则
						{
							required: true,
							message: '请填写密码',
							trigger: ['blur', 'change']
						}
					],
					Phone: [{
							required: true,
							message: '请输入手机号',
							trigger: ['change', 'blur'],
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// uni.$u.test.mobile()就是返回true或者false的
								return uni.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['change', 'blur'],
						}
					]
				}
			}
		},
		onReady() {
			// 设置表单校验规则 否则表单校验无效
			this.$refs.uLoginForm.setRules(this.rules);
		},
		methods: {
			btn_yes(){
				this.show=false;
				uni.redirectTo({
					url: '/pages/user/loginAndReg/login'
				});
			},
			btn_no(){
				this.show=false
			},
			sureRegister() {
				this.$refs.uLoginForm.validate().then(res => {
					uni.$u.http.post(this.$api +'/users/isRegister', {
						Phone: this.loginForm.Phone
					}).then(res => {
						if (res.data.length) {
							uni.showToast({
								title: '手机号已经注册',
								icon: 'none'
							});
						} else {
							uni.request({
								url:  this.$api + '/users/addUser',
								data: {
									userName: this.loginForm.userName,
									userPwd: this.loginForm.userPwd,
									Phone: this.loginForm.Phone
								},
								method: 'post',
								header: {
									'content-type': 'application/json'
								},
								success: (res) => {
									this.show = true
								}
							})
						}
					})
				})
			},
			passwordF_B() {
				this.hideEyes = !this.hideEyes;
			},
			formSubmit(e) {
				console.log(e.detail.value);
			}
		}
	}
</script>
<style>
	page {
		background-color: #FFE4B5;
	}
</style>
<style scoped >
	.warp {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 100%;
	}

	.rect {
		width: 220px;
		height: 80px;
		background-color: #F0F0F0;
		border-radius: 25px;
	}

	.btn_yes,
	.btn_no {
		margin: 5px;
		padding: 5px 10px;
		display: inline;
	}

	.login_register {
		text-align: center;
		margin-top: 10px;
	}

	.btn_login,
	.btn_register {
		margin: 5px;
		padding: 10px 30px;
		display: inline;
	}

	.login-form {
		margin: -9px 10px 0 10px;
		background: #007AFF;
		border-radius: 10px;
		background-color: #EEEEEE;
		box-shadow: 0 2px 10px #9B9B9B;
		padding: 20px;
	}

	.form-input input {
		background: #ffffff;
		border-radius: 5px;
		height: 40px;
		margin: 20px 0;
		padding: 0 10px;
	}

	.login-img {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 108px;
		margin-top: 100rpx;
	}

	.owl-login {
		width: 211px;
		height: 108px;
		background-image: url('');
		background-repeat: no-repeat;
		background-size: 100%;
		position: absolute;
	}

	.owl-login .hand {
		width: 34px;
		height: 34px;
		-webkit-border-radius: 40px;
		border-radius: 40px;
		background-color: #472d20;
		transform: scaleY(0.6);
		transition: 0.3s ease-out;
		position: absolute;
		left: 14px;
		bottom: -8px;
	}

	.owl-login .hand.hand-r {
		left: 170px;
	}

	.owl-login .arms {
		top: 58px;
		position: absolute;
		width: 100%;
		height: 41px;
		overflow: hidden;
	}

	.owl-login .arms .arm {
		width: 40px;
		height: 65px;
		position: absolute;
		left: 20px;
		top: 40px;
		background-image: url("");
		background-size: 100%;
		transition: 0.3s ease-out;
		transform: rotate(-20deg);
	}

	.owl-login .arms .arm.arm-r {
		transform: rotate(20deg) scaleX(-1);
		left: 158px;
	}

	.owl-login.password .hand {
		transform: translateX(42px) translateY(-15px) scale(0.7);
	}

	.owl-login.password .hand.hand-r {
		transform: translateX(-42px) translateY(-15px) scale(0.7);
	}

	.owl-login.password .arms .arm {
		transform: translateY(-40px) translateX(40px);
	}

	.owl-login.password .arms .arm.arm-r {
		transform: translateY(-40px) translateX(-40px) scaleX(-1);
	}
</style>
